<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
include('../../includes/classes/core.php');
$error		= '';
$action 	= $_REQUEST['act'];
$year       = $_REQUEST[Year]>0? $_REQUEST[Year]:'2010';
$year1      = $_REQUEST[Year]>0? $_REQUEST[Year]:'2020';

$start_m	= $_REQUEST[start_m]==0? '01':$_REQUEST[start_m];
$end_m		= $_REQUEST[end_m] ==0?   '12':$_REQUEST[end_m];

$client		= $_REQUEST[client] ==0? '': "AND client.id=$_REQUEST[client]";

$start = "$year-$start_m-01";
$end    = "$year1-$end_m-31";
$period = $end_m-$start_m+1;



//echo "$start ----- $end";

$client_id	= $_REQUEST[client];
	switch ($action) {	
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $rResult=mysql_query("
    	    SELECT   client_objects.id,
                     cities.`name`,
                     client.`name` AS client,
                     client_objects.`name` AS client_object,                     
    	             count(DISTINCT DATE(rs_wdsl_status.end_date)) AS `reisebi`,
                     count(DISTINCT warehouse_detail.id) AS quantity,'',
    	             ROUND(count(DISTINCT warehouse_detail.id)/count(DISTINCT DATE(rs_wdsl_status.end_date)))  
            FROM    a_letter
            JOIN    warehouse ON a_letter.id = warehouse.a_letter_id
    	    JOIN    rs_wdsl_status ON a_letter.b_letter_overhead_id=rs_wdsl_status.id    
            JOIN    warehouse_detail ON warehouse.id = warehouse_detail.warehouse_id 
            JOIN    client_objects ON a_letter.client_object_id = client_objects.id
            JOIN    cities ON client_objects.region = cities.id
            JOIN    client ON client.id = client_objects.client_id
            JOIN    production ON production.id = warehouse_detail.prod_id
            WHERE   client_objects.region != 1 AND client_objects.region != 0 AND warehouse_detail.actived = 1 AND 
    	            DATE(rs_wdsl_status.end_date) BETWEEN  '$start' AND '$end' $client
            GROUP BY  client.id, client_objects.id
            ORDER BY  cities.id, client.id
                                    ");
			
			$data = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}

	        break;
    case 'chart':
        $count = $_REQUEST['count'];
        $rResult=mysql_query("
            SELECT   
            cities.`name`,
            count(DISTINCT a_letter.id) AS `request`,
            count(DISTINCT DATE(rs_wdsl_status.end_date)) AS `reisebi`,
            count(DISTINCT warehouse_detail.id) AS quantity,
            count(DISTINCT warehouse_detail.id)/count(DISTINCT DATE(rs_wdsl_status.end_date))   
            FROM    a_letter
            JOIN    warehouse ON a_letter.id = warehouse.a_letter_id
            JOIN    rs_wdsl_status ON a_letter.b_letter_overhead_id=rs_wdsl_status.id 
            JOIN    warehouse_detail ON warehouse.id = warehouse_detail.warehouse_id
            JOIN    client_objects ON a_letter.client_object_id = client_objects.id
            JOIN    cities ON client_objects.region = cities.id
            JOIN    client ON client.id = client_objects.client_id
            JOIN    production ON production.id = warehouse_detail.prod_id
            WHERE   client_objects.region != 1 AND client_objects.region != 0 AND warehouse_detail.actived = 1 AND
            DATE(rs_wdsl_status.end_date) BETWEEN  '$start' AND '$end'  $client 
            GROUP BY  cities.id
            ORDER BY reisebi DESC
            ");
            	
            $data = array(
		"aaData"	=> array()
            );
            	
            while ( $aRow = mysql_fetch_array( $rResult ) )
            {
  
    				$data[xAxis][] = $aRow[0];
    				$data[series][data][0][] = (float)$aRow[2];
    		//		$data[series][data][1][] = (float)$aRow[4];
    			}
    
    	        break;
    	        
        case 'get_list1':
            $count = $_REQUEST['count'];
            $rResult=mysql_query("
                SELECT
                warehouse.id,
                DATE(rs_wdsl_status.end_date),
                CONCAT('ა - ',a_letter.id),
                DATE(a_letter.request_date),
                count(DISTINCT warehouse_detail.id) AS quantity,
                ''
                FROM    a_letter
                JOIN    warehouse ON a_letter.id = warehouse.a_letter_id
                JOIN    rs_wdsl_status ON a_letter.b_letter_overhead_id=rs_wdsl_status.id
                JOIN    warehouse_detail ON warehouse.id = warehouse_detail.warehouse_id
                JOIN    client_objects ON a_letter.client_object_id = client_objects.id
                JOIN    cities ON client_objects.region = cities.id
                JOIN    client ON client.id = client_objects.client_id
                JOIN    production ON production.id = warehouse_detail.prod_id
                WHERE   client_objects.region != 1 AND client_objects.region != 0 AND warehouse_detail.actived = 1 AND
                DATE(rs_wdsl_status.end_date) BETWEEN  '$start' AND '$end' $client AND client_objects.id='$_REQUEST[id]'
                GROUP BY   DATE(rs_wdsl_status.end_date), a_letter.id
                ORDER BY   a_letter.id DESC
                ");
        
            $data = array(
            "aaData"	=> array()
            );
        
            while ( $aRow = mysql_fetch_array( $rResult ) )
            {
            $row = array();
            for ( $i = 0 ; $i < $count ; $i++ )
            {
            /* General output */
                $row[] = $aRow[$i];
            }
            $data['aaData'][] = $row;
            }
             
            break;
    
	    case 'get_edit_page':	       
	        $page		= '
	        
		<div id="dialog-form">	
			<fieldset>
	          <legend>ლოგები</legend>	        
			    <div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area" class="inner_buttons">							
							</div>
				            <div id="dynamic">
				                <table class="display" id="inner_table">
                                    <thead>
                                        <tr id="datatable_header">
                                            <th>ID</th>
	                                        <th style="width: 100px;">რეისის<br>თარიღი</th>    
                                            <th style="width: 100px;">ბარათი</th>
                                            <th style="width: 100px;">შეკვეთების<br>თარიღი</th>                                                                           
                                            <th style="width: 100px;">კარტრიჯი</th>
                                            <th style="width: 100px;">პრინტერი</th>                           
                                                                               
                                        </tr>
                                    </thead>
                                    <thead>
                                        <tr class="search_header">
                                            <th class="colum_hidden">
                                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                                            </th>
                                            <th>
                                                <input type="text" name="search_type" value="ფილტრი" />
                                            </th>
	                                        <th>
                                                <input type="text" name="search_type" value="ფილტრი" />
                                            </th>
                                             <th>
                                                <input type="text"  name="search_type" value="ფილტრი"  />
                                            </th>    
                                            <th>
                                                <input type="text" name="search_code" value="ფილტრი"  />
                                            </th>                            
                                            <th>
                                               <input type="text"  name="search_model" value="ფილტრი" />                              
                                            </th>                      
                                        </tr>
                                    </thead>                       
                                    <tfoot>
                                        <tr>
                                            <th>&nbsp;</th>
	                                        <th>&nbsp;</th>    
                                            <th>&nbsp;</th>
                                            <th>&nbsp;</th>  
                                            <th>&nbsp;</th>                         
                                            <th>&nbsp;</th>

                                        </tr>
                                    </tfoot>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
			</fieldset>
	    </div>';
	        
	        $data 		= array('page'	=> $page);
	        $data['error'] = $error;	        
	        
	        
	        break;
	    default:
	       $error = "action null";
	}


echo json_encode($data);

?>